package com.zxing.decoding;

import android.util.Log;

import com.ciwong.libs.utils.CWLog;
import com.google.zxing.ResultPoint;
import com.zxing.camera.CameraManager;

/**
 * 答题卡检测工具 拍照答题卡时用到
 * Created by res-zwyan on 2015/7/31.
 */
public class AnswerCardCheckUtil
{
    private final String TAG = AnswerCardCheckUtil.class.getSimpleName();

    /**
     * 误差
     */
    private float errorRange = 0.09f;

    /**
     * 分份 ；值越大 检测越严格
     */
    private float part = 4.5f;

//    public static int minX = 0;
//
//    public static int maxX = 0;
//
//    public static int minY = 0;
//
//    public static int maxY = 0;

    /**
     * 判断答题卡距离是否合适
     * 判断逻辑是 答题卡四个点要与扫描框的四个角靠近 对上
     * @param points
     * @param width
     * @param height
     * @return true 答题卡与手机屏幕可以接受  false的话不符合要求
     */
    public boolean checkDistanceIsGood(ResultPoint[] points, int width,
            int height)
    {
//        if (part <= 2)
//        {
//            throw new IllegalArgumentException("part is error");
//        }
//        int minX = (int) (width / part), maxX = width - minX;
//        int minY = (int) (height / (part + 1)), maxY = height - minY;
//        this.minX = minX;
//        this.maxX = maxX;
//        this.minY = minY;
//        this.maxY = maxY;
//        CWLog.e("checkDistanceIsGood", "width = " + width + " height = "
//                + height + " minX = " + minX + " " + maxX + " | " + minY
//                + " , " + maxY);
//        CWLog.e("checkDistanceIsGood", "prewidth = "
//                + CameraManager.previewRect.width() + " preheight = "
//                + CameraManager.previewRect.height() + " minX = " + minX + " "
//                + maxX + " | " + minY + " , " + maxY);
//
//        for (ResultPoint point : points)
//        {
//            float x = point.getX();
//            float y = point.getY();
//            CWLog.e("checkDistanceIsGood", "点：x = " + x + " y=" + y);
//            if ((x >= minX && x <= maxX) || (y >= minY && y <= maxY))
//            {
//                return false;
//            }
//        }
        return true;
    }

    /**
     * 判断拍答题卡时答题卡是否是正的 点到边的距离 两两相等
     *
     * @param points
     * @param width
     * @param height
     * @return
     */
    public boolean checkIsStraight(ResultPoint[] points, int width, int height)
    {
        if (points != null)
        {
            int size = points.length;
            float[] ratioXArray = new float[size];
            float[] ratioYArray = new float[size];
            int i = 0;
            for (ResultPoint point : points)
            {
                float ratioX = getFormat(point.getX() / height, 100);
                float ratioY = getFormat(point.getY() / width, 100);
                ratioXArray[i] = ratioX;
                ratioYArray[i] = ratioY;
                i++;
                Log.e(TAG, "x y ==== " + ratioX + "  " + ratioY);
            }
            boolean bx = judgeEqual(ratioXArray);
            boolean by = judgeEqual(ratioYArray);
            // Log.e(TAG, "bx by ==== " + bx + "  " + by);
            if (bx && by)
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断values中两两相等 允许有误差 {@link #errorRange}
     *
     * @param values
     *            值数组
     * @return true 相等
     */
    private boolean judgeEqual(float[] values)
    {
        if (values == null || values.length % 2 != 0)
        {
            return false;
        }
        int pairCount = values.length / 2;
        int count = 0;
        for (int i = 0; i < values.length; i++)
        {
            float currentValue = values[i];
            if (currentValue == -1)
            {
                continue;
            }
            for (int j = i + 1; j < values.length; j++)
            {
                float afterValue = values[j];
                if (afterValue == -1)
                {
                    continue;
                }
                float delValue = Math.abs(afterValue - currentValue);
                if (Float.compare(delValue, errorRange) <= 0)
                {
                    count++;
                    values[j] = -1;
                    break;
                }
            }
        }
        if (pairCount == count)
        {
            print(values);
            return true;
        }
        print(values);
        return false;
    }

    /**
     * 取小数点后多少位 10是一位  100是后两位 以此类推
     * @param ff
     * @param radio 100
     * @return
     */
    private float getFormat(float ff, int radio)
    {
        return (float) Math.round(ff * radio) / radio;
    }

    private void print(float[] values)
    {
        String str = "";
        for (int i = 0; i < values.length; i++)
        {
            str += values[i] + " ";
        }
        CWLog.e(TAG, "=" + str);
    }

}
